<template>
  <div>
    <el-dialog
      :title="title"
      width="652px"
      :visible.sync="isShow"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="产品集编码" prop="code" v-if="form.id">
          <el-input disabled v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="产品集名称" prop="name">
          <el-input placeholder="请输入产品集名称" v-model="form.name" maxlength="24"></el-input>
        </el-form-item>
        <el-form-item label="产品类型" prop="productType">
          <el-select v-model="form.productType">
            <el-option
              v-for="(item, index) in productTypeList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属项目" prop="appCode">
          <el-select v-model="form.appCode">
            <el-option
              v-for="(item, index) in belongProjectList"
              :key="index"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status">
            <el-option
              v-for="(item, index) in statusList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="submit()">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { TYPE_NAV_LIST, STATUS_LIST } from '@/constants/baseModel'
export default {
  name: 'AddEditPop',
  data() {
    return {
      productTypeList: TYPE_NAV_LIST.filter(item => item.label !== '全部'),
      belongProjectList: [],
      statusList: STATUS_LIST,
      isShow: false,
      rules: {
        name: [{ required: true, message: '请填写产品集名称' }],
        productType: [{ required: true, message: '请选择产品类型' }],
        appCode: [{ required: true, message: '请选择所属项目' }],
      },
      form: {
        id: '',
        // 编码
        code: '',
        // 名称
        name: '',
        // 产品类型
        productType: '',
        // 所属项目
        appCode: '',
        // 状态, 默认启用
        status: '1',
      },
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑产品集' : '新增产品集'
    },
  },
  methods: {
    show(item) {
      this.isShow = true
      this.form.id = (item && item.id) || ''
      this.form.code = (item && item.code) || ''
      this.form.name = (item && item.name) || ''
      this.form.productType = (item && Number(item.productType)) || ''
      this.form.appCode = (item && item.appCode) || ''
      this.form.status = (item && item.status) || '1'
      this.getSourceListRequest()
    },
    submit() {
      this.$refs['form'].validate(async valid => {
        if (!valid) return
        await this.submitRequest()
        this.resetData()
        this.$emit('popClose', true)
      })
    },
    handleClose() {
      this.resetData()
    },
    resetData() {
      this.isShow = false
      this.form.id = ''
      this.form.code = ''
      this.form.name = ''
      this.form.productType = ''
      this.form.appCode = ''
      this.form.status = '1'
      this.$refs['form'].resetFields()
    },
    async submitRequest() {
      const url = this.form.id ? `/product_sets/${this.form.id}/update` : `/product_sets/add`
      const method = this.form.id ? 'put' : 'post'
      this.$http[method](url, this.form)
    },
    async getSourceListRequest() {
      this.belongProjectList = await this.$http.get(`/app/list`)
    },
  },
}
</script>

<style lang="scss" scoped>
.btn {
  width: 400px;
}
::v-deep {
  .el-input {
    width: 400px;
  }
  .el-input__inner {
    width: 400px;
  }
  .el-textarea {
    width: 400px;
  }
}
</style>
